package com.heimatest.io_marquee_random_named;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;

public class MyJFrame extends JFrame {
    JPanel center;

    public MyJFrame() throws Exception{
        init();
        initCenter();
        initSouth();
        this.setVisible(true);
    }

    private void init() {
        this.setTitle("Random Named Example");
        this.setSize(600, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLayout(new BorderLayout());
    }

    private void initCenter() throws Exception{
        center = new JPanel();
        center.setLayout(new FlowLayout(FlowLayout.LEFT));
//        for (int i = 0; i < 30; i++) {
//            JButton jbb = new JButton("张三" + i);
//            jbb.setBackground(new Color(255, 255, 255));
//            center.add(jbb);
//        }
        InputStreamReader reader = new InputStreamReader(MyJFrame.class.getClassLoader().getResourceAsStream("names.txt"), "UTF-8");
        BufferedReader br = new BufferedReader(reader);
        String name;
        while((name = br.readLine()) != null){
            JButton jbb = new JButton(name);
            jbb.setBackground(new Color(255, 255, 255));
            center.add(jbb);
        }
        br.close();
        this.add(center, BorderLayout.CENTER);
    }

    private void initSouth() {
        JPanel south = new JPanel();
        JButton jb = new JButton("抽奖");
        Random ra = new Random();
        jb.addMouseListener(new MouseAdapter() {
            Thread t = null;
            private Component comp = null;

            public void mouseClicked(MouseEvent e) {
                if (jb.getText().equals("抽奖")) {
                    jb.setText("停止");
                    Component[] names = center.getComponents();
                    for (int i = 0; i < names.length; i++) {
                        names[i].setBackground(Color.WHITE);
                    }
                     t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                int index = ra.nextInt(names.length);
                                names[index].setBackground(Color.RED);
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException ex) {
                                    throw new RuntimeException(ex);
                                }
                                names[index].setBackground(Color.WHITE);
                            }
                        }
                    });
                    t.start();


                } else {
                    jb.setText("抽奖");
                    t.stop();
                }

            }
        });
        south.add(jb);
        this.add(south, BorderLayout.SOUTH);
    }
}
